﻿using System;
using System.Diagnostics;
using DotNetRepository.Cache;

namespace DotNetRepository.LinqToSql
{
    /// <summary>
    /// This action is meant to be used exclusively by CacheItems stored in the EntityCache.  It converts
    /// the CacheItem to a WeakReference and moves it to the BusinessObjectWeakReferenceCache.
    /// </summary>
    [Serializable]
    internal class EntityCacheRefreshAction<TEntity> : ICacheItemRefreshAction<Guid,EntityCacheItem<TEntity>> where TEntity : class, IEntity
    {
        #region ICacheItemRefreshAction Members

        public void Refresh(Guid removedKey, EntityCacheItem<TEntity> expiredValue, CacheItemRemovedReason removalReason)
        {
            if (removalReason == CacheItemRemovedReason.Removed)
            {
                if (Settings.Default.EnableCacheTrace)
                    Debug.WriteLine(String.Format("{0} manually removed from strong cache.",expiredValue));
            }
            else
            {
                EntityCache<TEntity>.Instance.AddWeak(expiredValue);
            }
        }

        #endregion
    }
}